package com.example.qinwen.musicplayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button playButton, pauseButton, volumeUpButton, volumeDownButton;
    private MediaPlayer mediaPlayer;
    private float leftVolume = 0.5f, rightVolume = 1 - leftVolume;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = MediaPlayer.create(this, R.raw.howlongwilliloveyou);

        playButton = (Button) findViewById(R.id.play);
        pauseButton = (Button) findViewById(R.id.pause);
        volumeUpButton = (Button) findViewById(R.id.volumeUp);
        volumeDownButton = (Button) findViewById(R.id.volumeDown);

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.start();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer){
                        Toast.makeText(getApplicationContext(), "IM Done", Toast.LENGTH_SHORT);
                    }
                });
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.pause();
            }
        });


        volumeUpButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftVolume < 1) {
                    leftVolume += 0.1;
                    rightVolume += 0.1;
                    mediaPlayer.setVolume(leftVolume, rightVolume);
                }
            }
        });

        volumeDownButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftVolume > 0) {
                    leftVolume -= 0.1;
                    rightVolume -= 0.1;
                    mediaPlayer.setVolume(leftVolume, rightVolume);
                }
            }
        });


    }
}